/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.dao;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.HibernateBaseDao;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.model.Form;

/**
 * 建立日期 : 2013-4-25 上午11:22:43<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class FormDao extends HibernateBaseDao<Form> {

	@SuppressWarnings("unchecked")
	public Form findById(String appId) {
//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "f.companyId='" + comId + "' and ";
		String hql = "from Form f where "+condition+"f.formId=?";
		List<Form> listForm = createQuery(hql, new Object[]{appId}).list();
		if(listForm.size() > 0){
			return listForm.get(0);
		}
		return null;
	}
	
	/**
	 * 获取form模板数据并插入表里
	 * @param companyId
	 */
	public void addForm(String companyId) throws HibernateException{
		SQLQuery sql = (SQLQuery) getSession().createSQLQuery("insert into wf_form w (w.FORM_ID,w.TEMPLET_BYTE,w.TEMPLET_PATH,w.TEMPLET_STORAGE_TYPE,w.FORM_NAME,w.COMPANY_ID,w.TEMPLATE_VIEW_BYTE,w.MOBILE_TEMPLATE_BYTE) " +
				" select :companyId||sw.FORM_ID,sw.TEMPLET_BYTE,sw.TEMPLET_PATH,sw.TEMPLET_STORAGE_TYPE,sw.FORM_NAME,:companyId,sw.TEMPLATE_VIEW_BYTE,sw.MOBILE_TEMPLATE_BYTE " +
				" from sys_wf_form sw").setString("companyId", companyId);
		sql.executeUpdate();
	}
	/**
	 * 获取sys_wf_form表中的数据
	 * 建立日期 : Oct 17, 2013 9:46:33 AM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: <br>
	 * 描述: <br>
	 * 1 <br>
	 * 2 <br>
	 */
	public List<Form> getSysForm(){
		
		String hql = "select f.form_id,f.templet_byte,f.templet_path,f.template_view_byte,f.mobile_template_byte from sys_wf_form f";
		List<Object> objects = createQuery(hql).list();
		List<Form> formList = new ArrayList<Form>();
		
		for (Iterator it = objects.iterator(); it.hasNext();) {
			Object[] a = (Object[]) it.next();
			try {
				Form form = new Form();
				form.setFormId(a[0].toString());
				form.setTempletByte(a[1].toString().getBytes("UTF-8"));
				form.setTempletPath(a[2].toString());
				form.setTemplateViewByte(a[3].toString().getBytes("UTF-8"));
				form.setMobileTemplateByte(a[4].toString().getBytes("UTF-8"));
				
				formList.add(form);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return formList;
	}
	/**
	 * 修改form实体
	 * 建立日期 : Oct 17, 2013 11:44:06 AM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: <br>
	 * 描述: <br>
	 * 1 <br>
	 * 2 <br>
	 */
	public void updateSysForm(Form form){
		String sql = "update sys_wf_form f set f.templet_byte='"+form.getTempletByte()+
					 "',f.templet_path='"+form.getTempletPath()==null?"":form.getTempletPath()+
					 "',f.template_view_byte='"+form.getTemplateViewByte()+
					 "',f.mobile_template_byte='"+form.getMobileTemplateByte()+
					 "' where f.form_id='"+form.getFormId()+"'";
		createSQLQuery(sql);
	}
	
}
